package com.tcm.ucenter.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.utils.R;
import com.tcm.ucenter.domain.dto.BankAccountDTO;
import com.tcm.ucenter.service.BankAccountService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RequestMapping("/ucenter")
@RestController
public class BankAccountController {

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private BankAccountService bankAccountService;



    @GetMapping("/bank/view")
    public R view(@RequestParam("userId") Long userId) {
        BankAccountDTO bankAccountDTO;
        try {
            bankAccountDTO = bankAccountService.findById(userId);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success(bankAccountDTO);
    }

    @PostMapping("/bank/save")
    public R save(@Validated @RequestBody BankAccountDTO userAccountDto) {
        try{
            bankAccountService.save(userAccountDto);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

}

